#include "tobject.h"

TObject::TObject(TGraphic* graphic)
{
    _graphic = graphic;
    _state = 0;
    _id = 0;
    IsAlive = true;
}

TObject::~TObject(){
    if(_graphic != NULL) delete _graphic;
}

bool TObject::checkIfAlive(){
    return IsAlive;
}

void TObject::tick(){
    move();
}

const vector2d& TObject::getPosition() const{
    return _position;
}

char TObject::getType() const{
    return _type;
}

TObject* TObject::setPosition(vector2d position){
    _position = position;
    return this;
}

TGraphic * TObject::getGraphic() const{
    return _graphic;
}

int TObject::getState() const{
    return _state.value();
}

TObject* TObject::setState(int state){
    _state = state;
    return this;
}

int TObject::getId() const{
    return _id;
}

TObject* TObject::setId(int id){
    _id = id;
    return this;
}


void TObject::setVelocity(vector2d velocity){
    _velocity = velocity;
}

const vector2d& TObject::getSize() const{
    return _size;
}

TObject* TObject::setSize(vector2d size){
    _size = size;
    return this;
}

QString TObject::dump(){
    return QString::number(_id) + QString(":") + QString::number(_state.value(),'g',2);
}


Rectangle TObject::getRectangle() const{
    // вообще-то тут хорошо бы учитывать "повернутость" объекта
    Rectangle a;
    a.left   = this->getPosition().x() - this->getSize().x()/2;
    a.right  = this->getPosition().x() + this->getSize().x()/2;
    a.bottom = this->getPosition().y() + this->getSize().y()/2;
    a.top    = this->getPosition().y() - this->getSize().y()/2;

    return a;
}

bool TObject::rectangleCollision(const TObject& one, const TObject& two){
    Rectangle r1 = one.getRectangle(),r2 = two.getRectangle();
    return r1.left<=r2.right && r2.left<=r1.right && r1.top<=r2.bottom && r2.top<=r1.bottom;
}

bool TObject::interaction(TObject& one, TObject& two){
    switch(one.getType()){
        case 'w':
            switch(two.getType()){
                case 't': return TObject::interactionWallTank  (one,two);
                case 'b': return TObject::interactionWallBullet(one,two);
            default: return false;
            }
        case 't':
            switch(two.getType()){
                case 'w': return TObject::interactionWallTank  (two,one);
                case 't': return TObject::interactionTankTank  (two,one);
                case 'b': return TObject::interactionBulletTank(two,one);
                default: return false;
            }
        case 'b':
            switch(two.getType()){
                case 'w': return TObject::interactionWallBullet  (two,one);
                case 't': return TObject::interactionBulletTank  (one,two);
                case 'b': return TObject::interactionBulletBullet(one,two);
                default: return false;
            }
        default: return false;
    }
}

bool TObject::interactionWallTank     (TObject& wall,   TObject& tank){
    wall.IsAlive = false;
    return false;
}

bool TObject::interactionWallBullet   (TObject& wall,   TObject& bullet){
    bullet.IsAlive = false;
    return true;
}

bool TObject::interactionBulletTank   (TObject& bullet, TObject& tank){
    bullet.IsAlive = false;
    return true;
}

bool TObject::interactionTankTank     (TObject& tank, TObject& tank2){
    return false;
}

bool TObject::interactionBulletBullet (TObject& bullet, TObject& bullet2){
    // разкомментирование приводит к локальному коллапсу %)
//    bullet.IsAlive = false;
//    bullet2.IsAlive = false;
    return false;
}
